<?php
	class ProductBO 
	{   
		private $product;

		function __construct($id = null)
		{
			if(isset($id))
				$this->product=R::load('product',$id);
			else
				$this->product=R::dispense('product');			
		}

		public function getName()
		{
			return $this->product->name;
		}
		
		public function getID()
		{
			return $this->product->id;
		}
		
		public function getSKU()
		{
			return $this->product->sku;
		}
		
		public function isOnline()
		{
			if($this->product->online == 1)
			{
				return true;
			}
			else
			{
				false;
			}
		}
                
                public function getDiscountPrice()
                {
                    $prices= $this->product->ownProductprice;
		        foreach($prices as $k => $price)
			{
				if($price->type == 'DiscountedPrice')
				 return $price->price;
			}
                }

                public function getPrice()
		{
			$prices= $this->product->ownProductprice;
			foreach($prices as $k => $price)
			{
				if($price->type == 'SalePrice')
				 return $price->price;
			}
		}
		
		public function getImage()
		{
			$images= $this->product->ownProductimage;
			foreach($images as $k => $image)
			{
				if($image->imageview == 'Default' && $image->imagetype == 'L')
				 return $image->imagepath;
			}
		}

		public function getPromotion()
		{
			$promotions =$this->product->ownSales;
			$now = time();
			foreach($promotions as $k => $promotion)
			{
				
				if($promotion->start_date >= $now || $promotion->end_date <=$now)
				 return $promotion;
			}
		}
		
		public function getCategoryName()
		{
			$name="";
			$categories = $this->product->sharedCategory;
			
			foreach($categories as $k => $category)
			{
				$name=$name.$category->name."<br>";
			}
			return  $name;
		}
		
		public function getSeoID()
		{
			return $this->product->seoid;
		}
		
		public function getDesc()
		{
			return $this->product->desc;
		}
		
		public function getLongDesc()
		{
			return $this->product->longdesc;
		}
		
		public function getBrand()
		{
			return $this->product->brand;
		}public function getColor()
		{
			return $this->product->color;
		}
		
		public function getPartner()
		{
			return $this->product->partner;
		}
		
		public function getSize()
		{
			return $this->product->size;
		}
		
		public function getWeight()
		{
			return $this->product->weight;
		}
		public function getPacking()
		{
			return $this->product->packing;
		}

		function getCA($name)
		{
				$cas=$this->product->withCondition('name = ?',array($name))->ownProductca;
						
				if(isset($cas) && count($cas) >= 1)
				{
					return reset($cas)->value;
				}
			return '';
		}
		
		public function getCAs()
		{
			return $this->product->ownProductca;
		}
				
		public function getImages()
		{
			return $this->product->ownProductimage;
		}
		
		public function getPrices()
		{
			return $this->product->ownProductprice;
		}
		
		public function getPromotions()
		{
			return $this->product->ownSales;
		}
		
		public function getCategories()
		{
			return $this->product->sharedCategory;
		}
		
		public function getVariationBOs()
		{
			$productBOs=array();
			$pvs =  $this->product->alias('master')->ownProductvariation;

			foreach($pvs as $k => $pv)
			{
				$variation=$pv->fetchAs('product')->variation;
					array_push($productBOs,BOFactory::getProductBO($variation->id));
				
			}		
				usort($productBOs, function($a, $b)
				{
					return strcmp($a->getSize(), $b->getSize());
				});			
			return $productBOs;
		}

	}